package netTracker.network;

import netTracker.NetTracker;

/**
 * A message handler for messages specifically sent to a session server.
 * @author David Cummings
 */
public class ServerMessageHandler implements MsgHandler {
	
	Message m;
	NetTracker netTracker;
	
	public ServerMessageHandler(NetTracker netTracker){
		this.netTracker = netTracker;
	}
	
	public void handleMsg(Message m) throws PostalException {
		this.m = m;
		
		if(m.getTag().equals("SELECTCELL")){
			String[] coordinates = m.getMsg().split(",");
			selectOutline(coordinates);
		}else if(m.getTag().equals("MOVE")){
			String[] coordinates = m.getMsg().split(",");
			moveOutline(coordinates);
		}else if(m.getTag().equals("JOINSESSION")){
			netTracker.getPO().addAddress(m.getSrc());	// If client joins, add them to address book.
			netTracker.informClient(m.getSrc());			// Inform the client of the current song status.
		}
	}
	
	public void selectOutline(String[] coordinates){
		netTracker.selectOutline(m.getSrc().getID(),Integer.parseInt(coordinates[0]),
			Integer.parseInt(coordinates[1]));
	}
	
	public void moveOutline(String[] coordinates){
		netTracker.moveOutline(m.getSrc().getID(),Integer.parseInt(coordinates[0]),
				Integer.parseInt(coordinates[1]));
	}
}
